package com.liqi.items.service.impl;

import com.liqi.common.annotation.DataSource;
import com.liqi.common.enums.DataSourceType;
import com.liqi.common.utils.DateUtils;
import com.liqi.items.domain.TCfgService;
import com.liqi.items.domain.TCfgSubject;
import com.liqi.items.mapper.TCfgServiceMapper;
import com.liqi.items.mapper.TCfgBranchMapper;
import com.liqi.items.service.ITCfgServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 事项Service业务层处理
 *
 * @author ruoyi
 * @date 2022-04-08
 */
@Service
public class TCfgServiceServiceImpl implements ITCfgServiceService
{
    @Autowired
    private TCfgServiceMapper tCfgServiceMapper;

    @Autowired
    private TCfgBranchMapper tCfgBranchMapper;

    /**
     * 查询事项
     *
     * @param id 事项主键 添加返回网点信息
     * @return 事项
     */
    @Override
    public TCfgService selectTCfgServiceById(Long id)
    {
        TCfgService tCfgService = tCfgServiceMapper.selectTCfgServiceById(id);
        String[] branchs = tCfgBranchMapper.selectTCfgBranchListByServiceCode(tCfgService.getServiceCode());
       if(branchs.length > 0){
        tCfgService.setBranchCodes(branchs);
       }
        return tCfgService;
    }

    /**
     * 查询事项列表
     *
     * @param tCfgService 事项
     * @return 事项
     */
    @Override
    public List<TCfgService> selectTCfgServiceList(TCfgService tCfgService)
    {
        return tCfgServiceMapper.selectTCfgServiceList(tCfgService);
    }

    /**
     * 新增事项
     *
     * @param tCfgService 事项
     * @return 结果
     */
    @Override
    public int insertTCfgService(TCfgService tCfgService)
    {
        tCfgService.setCreateTime(DateUtils.getNowDate());
        return tCfgServiceMapper.insertTCfgService(tCfgService);
    }

    /**
     * 修改事项
     *
     * @param tCfgService 事项
     * @return 结果
     */
    @Override
    public int updateTCfgService(TCfgService tCfgService)
    {
        tCfgService.setUpdateTime(DateUtils.getNowDate());
        return tCfgServiceMapper.updateTCfgService(tCfgService);
    }


    /**
     *     抽取list批量插入网点构造方法,每个事项跟传入的所有网点配对插入关联表
     */
    public List<TCfgService> getServiceList(TCfgService tCfgService, String[] branchCodes){
        List <TCfgService> list = new ArrayList<>();
        for (int i = 0; i < branchCodes.length; i++) {
            TCfgService temp = new TCfgService();
            temp.setServiceCode(tCfgService.getServiceCode());
            temp.setServiceAbstract(branchCodes[i]);
            list.add(temp);
        }
        return list;
    }

    /**
     * 批量删除事项
     *
     * @param ids 需要删除的事项主键
     * @return 结果
     */
    @Override
    public int deleteTCfgServiceByIds(Long[] ids)
    {
        return tCfgServiceMapper.deleteTCfgServiceByIds(ids);
    }

    /**
     * 批量软删除事项
     *
     * @param map 需要删除的事项主键
     * @return 结果
     */
    @Override
    public int SoftdeleteTCfgServiceByIds(Map<String, Object> map)
    {
        return tCfgServiceMapper.softdeleteTCfgServiceByIds(map);
    }

    /**
     * 删除事项信息
     *
     * @param id 事项主键
     * @return 结果
     */
    @Override
    public int deleteTCfgServiceById(Long id)
    {
        return tCfgServiceMapper.deleteTCfgServiceById(id);
    }

    /**
     * 通过主题编码 查询事项列表
     *
     * @param subjectCode 主题编码
     * @return 事项集合
     */
    @Override
    public List<TCfgService> selectTCfgServiceByItemCode(TCfgSubject tCfgSubject) {
        return tCfgServiceMapper.selectTCfgServiceByItemCode(tCfgSubject);
    }

    /**
     * 通过主题 查询出排除已于当前主题关联的事项列表
     *
     * @param subjectCode 主题编码
     * @return 事项集合
     */
    @Override
    public List<TCfgService> selectTCfgServiceListExcludeExistItemCode(String branchCode, String serviceName, String subjectCode,String deptName) {
        return tCfgServiceMapper.selectTCfgServiceListExcludeExistItemCode(branchCode, serviceName, subjectCode, deptName);
    }

    /**
     * 获取关联事项
     */
    /*@Override
    @DataSource(DataSourceType.SLAVE)
    public List<Map<String, Object>> selectReactTCfgServiceList() {
        return tCfgServiceMapper.selectReactTCfgServiceList();
    }*/
    /**
     * 新增事项时若勾选网点,则调用该方法进行批量插入关联表
     * @return
     */
    @Override
    public int batchServiceAndBranch(List<TCfgService> tCfgServices) {
        return tCfgServiceMapper.batchServiceAndBranch(tCfgServices);
    }

    /**
     * 编辑事项时若勾选网点,则调用该方法进行删除已有插入关联表
     * @param
     * @return
     */
    @Override
    public int deleteBranchsByServiceCode(String serviceCode) {
        return tCfgServiceMapper.deleteServiceAndBranchByServiceCode(serviceCode);
    }

    /**
     * 根据事项id数组删除关联表得网点事项关联
     * @param ids
     */
    @Override
    public void batchDeleteServiceandBranch(Long[] ids) {
        String[] serviceCodes = tCfgServiceMapper.selectTCfgServiceCodeByIds(ids);
        if (serviceCodes.length > 0){
            for (int i = 0; i < serviceCodes.length; i++) {
                tCfgServiceMapper.deleteServiceAndBranchByServiceCode(serviceCodes[i]);
                tCfgServiceMapper.deleteServiceAndSubjectByServiceCode(serviceCodes[i]);
            }
        }
    }

    @Override
    public List<String> getServiceByIds(Long[] ids) {
        return tCfgServiceMapper.getServiceByIds(ids);
    }

    /**
     * 获取主库所有事项的事项编码serviceCode
     * @return
     */
    @Override
    public List<String> selectServiceCode() {
        return tCfgServiceMapper.selectServiceCode();
    }

    /**
     * 查找已经存在设备管理后台的关联事项编码
     * @return
     */
   /* @Override
    public String[] selectReactTCfgServiceCodesExistInDeviceDB() {
        return tCfgServiceMapper.selectReactTCfgServiceCodesExistInDeviceDB();
    }*/
    /**
     * 根据事项编码数组过滤已添加的事项
     * @param reactServiceIdsExist
     * @return
     */
    /*@Override
    @DataSource(DataSourceType.SLAVE)
    public List<Map<String, Object>> selectReactTCfgServiceListFilterByServiceCodes(String[] reactServiceCodesExist) {
        return tCfgServiceMapper.selectReactTCfgServiceListFilterByServiceCodes(reactServiceCodesExist);
    }*/

    /**
     * 根据关联关系 过滤已关联过 的事项 ，即排除 主库所有事项的事项编码
     * @return
     */
    @Override
    @DataSource(DataSourceType.SLAVE)
    public List<Map<String, Object>> selectReactTCfgServiceListFilterByRelation(String serviceName, String serviceCode) {
        return tCfgServiceMapper.selectReactTCfgServiceListFilterByRelation(serviceName, serviceCode);
    }

    /**
     * 修改远程库的relation关联状态
     * @param map 所需要的 mybatis参数
     * @return
     */
    /*@Override
    @DataSource(DataSourceType.SLAVE)
    public int relationTCfgService(Map<String, String> map) {
        return tCfgServiceMapper.relationTCfgService(map);
    }*/

    @Override
    public int deleteServiceBranchInServiceCodes(Map<String, Object> map) {
        return tCfgServiceMapper.deleteServiceBranchInServiceCodes(map);
    }

    @Override
    public List<TCfgService> selectTCfgServiceByBranchCode(TCfgService tCfgService) {
        return tCfgServiceMapper.selectTCfgServiceByBranchCode(tCfgService);
    }

    @Override
    public List<TCfgService> selectTCfgServiceListExcludeExistBranchCode(String branchCode, String serviceName, String deptName) {
        return tCfgServiceMapper.selectTCfgServiceListExcludeExistBranchCode(branchCode,serviceName,deptName);
    }

    @Override
    public Integer count(String serviceCode) {
        return tCfgServiceMapper.count(serviceCode);
    }

    @Override
    public int updateByServiceCode(TCfgService tCfgService) {
        return tCfgServiceMapper.updateByServiceCode(tCfgService);
    }


}
